package views;

import presenters.SubmitItemPresenter;

import com.example.wheresmystuff.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * Activity for SubmitItem
 * 
 * @author Kelsey
 */
public class SubmitItemActivity extends Activity implements IFieldView {

	/** presenter associated with activity */
	private SubmitItemPresenter myPresenter;

	/**
	 * gets called when created
	 * 
	 * @bundle - the saved bundle instance
	 * @param savedInstanceState
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_submit_item);
		myPresenter = new SubmitItemPresenter(this);

	}

	/**
	 * gets called when createoptions menu happens
	 * 
	 * @param menu
	 *            - the menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.submit_item, menu);
		return true;
	}

	/**
	 * Advances
	 * 
	 * @param source
	 *            - the source
	 */
	@Override
	public void advance(View source) {
		Intent intent = new Intent(this, MainListActivity.class);
		startActivity(intent);
		finish();
	}

	/**
	 * Shows a toast
	 * 
	 * @param toast
	 *            - the string toast
	 */
	@Override
	public void showToast(String toast) {
		Toast.makeText(this, toast, Toast.LENGTH_SHORT).show();
	}

	/**
	 * helps make the keyboard prettier
	 * 
	 * @param ev
	 *            - the motionevent
	 */
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if (!isPointInsideView(ev.getRawX(), ev.getRawY(), getCurrentFocus())) {
			myPresenter.hideSoftKeyboard();
			return true;
		}
		return false;
	}

	/**
	 * checks to see if the point is inside the view
	 * 
	 * @param x
	 *            - x position
	 * @param y
	 *            - y position
	 * @param source
	 *            - source, the source
	 * @return if the point is inside the view
	 */
	private boolean isPointInsideView(float x, float y, View source) {
		int location[] = new int[2];
		source.getLocationOnScreen(location);
		int viewX = location[0];
		int viewY = location[1];

		if ((x > viewX && x < (viewX + source.getWidth()))
				&& (y > viewY && y < (viewY + source.getHeight()))) {
			return true;
		} else
			return false;
	}

	/**
	 * hides the keyboard view source - the source
	 */
	@Override
	public void hideSoftKeyboard(View source) {
		if (getCurrentFocus() != null && getCurrentFocus() instanceof EditText) {
			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(source.getWindowToken(), 0);
		}
	}

	/**
	 * does nothing
	 */
	@Override
	public void showProgressBar() {

	}

	/**
	 * gets called when submit click is clicked
	 * 
	 * @param source
	 *            - the source
	 */
	public void onSubmitClick(View source) {
		String desc = getDescriptionField().getText().toString();
		String name = getNameField().getText().toString();
		String location = getLocationField().getText().toString();
		String type = getTypeField().getSelectedItem().toString(); // will
																	// convert
																	// to enum
																	// value in
																	// presenter
		String category = getCategoryField().getSelectedItem().toString();
		myPresenter.onSubmitClick(desc, name, location, type, category, source);
	}

	/**
	 * gets EditText from description field
	 * 
	 * @return EditText from description field
	 */
	private EditText getDescriptionField() {
		return (EditText) findViewById(R.id.queryText);
	}

	/**
	 * gets EditText from name field
	 * 
	 * @return EditText from name field
	 */
	private EditText getNameField() {
		return (EditText) findViewById(R.id.editText2);
	}

	/**
	 * gets EditText from location field
	 * 
	 * @return EditText from location field
	 */
	private EditText getLocationField() {
		return (EditText) findViewById(R.id.editText3);
	}

	/**
	 * gets Spinner from type field
	 * 
	 * @return Spinner from type field
	 */
	private Spinner getTypeField() {
		return (Spinner) findViewById(R.id.spinner1);
	}

	/**
	 * gets Spinner from category field
	 * 
	 * @return Spinner from category field
	 */
	private Spinner getCategoryField() {
		return (Spinner) findViewById(R.id.spinner2);
	}

	/**
	 * gets clicked when on Cancelclick gets clicked
	 * 
	 * @param source
	 *            - the source
	 */
	public void onCancelClick(View source) {
		myPresenter.onCancelClick(source);
	}
}